﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.Business;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Business;

namespace Actuals
{
    public partial class AddProject : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                IBusinessTeam businessTeam = new BusinessTeam();
                IList<ITeam> teams = businessTeam.GetTeams();

                ddlTeam.DataSource = teams;
                ddlTeam.DataValueField = "TeamId";
                ddlTeam.DataTextField = "TeamDescription";
                ddlTeam.DataBind();
                ddlTeam.Items.Insert(0, "Select");

                if (Request.QueryString["Mode"] != null)
                {

                    string _mode = Request.QueryString["Mode"].ToString();
                    ViewState["Mode"] = _mode;
                    string _projectId = string.Empty;
                    if (Request.QueryString["ProjectId"] != null)
                    {
                        _projectId = Request.QueryString["ProjectId"].ToString();
                        hidProjectId.Value = _projectId;
                        PopulateProject(int.Parse(_projectId));
                    }

                }

            }
        }

        protected void PopulateProject(int projectId)
        {
            IBusinessProject businessProject = new BusinessProject();
            IProject project = businessProject.GetProject(projectId);
            txtDescription.Text = project.ProjectDescription;
            ddlTeam.SelectedValue = project.TeamId.ToString();

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                IProject project = new Project();
                project.ProjectDescription = txtDescription.Text;
                project.TeamId = int.Parse(ddlTeam.SelectedValue);

                IBusinessProject businessProject = new BusinessProject();

                if (ViewState["Mode"].ToString() == "Edit")
                {
                    project.ProjectId = int.Parse(hidProjectId.Value);
                    businessProject.UpdateProject(project);
                }
                else
                {
                    businessProject.AddProject(project);
                }
                lblMsg.Text = "Updated!";
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }


         

        }
    }
}